﻿using Inovout.Cloud.Applications.Security.Models;
using Inovout.Services;
using System;

namespace Inovout.Cloud.Applications.Security.Services
{
    public class ServerApplicationCredentialService : RepositoryService<ServerApplicationCredential>, IServerApplicationCredentialService
    {
        public override void Save(ServerApplicationCredential serverApplicationCredential)
        {
            serverApplicationCredential.ApplicationUser.Credentials.Add(serverApplicationCredential);
            serverApplicationCredential.ApiKey = Guid.NewGuid().ToString("N");
            base.Save(serverApplicationCredential);
        }

        public override void Delete(object id)
        {
            ServerApplicationCredential serverApplicationCredential = base.FindById(id);
            serverApplicationCredential.ApplicationUser.Credentials.Remove(serverApplicationCredential);
            base.Delete(id);
        }

        public void UpdateApiKey(ServerApplicationCredential serverApplicationCredential)
        {
            string apiKey = Guid.NewGuid().ToString("N");
            serverApplicationCredential.ApiKey = apiKey;
            serverApplicationCredential.ApplicationUser.Credentials.Add(serverApplicationCredential);
            base.Save(serverApplicationCredential);
        }   
    }
}
